package com.yyjj.hangqing.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.hangqing.HqNetUrl;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.activity.EditCoinActitivy;
import com.yyjj.hangqing.adapter.CacheAdatpter;
import com.yyjj.hangqing.adapter.ZdfAdpter;
import com.yyjj.hangqing.cache.CoinCache;
import com.yyjj.hangqing.entity.BaseTResp;
import com.yyjj.hangqing.entity.CacheEntity;
import com.yyjj.hangqing.entity.ZdEntity;
import com.yyjj.hangqing.entity.Zdentity1;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.utils.FastUtil;

/**
 * 自选
 */
public class CacheBiFragment extends BaseFragment {
    private LinearLayout noCacheLayout;
    private RecyclerView noRecyclerview;
    private TextView noHTv;
    private TextView noYjaddTv;
    private RelativeLayout cacheLayout;
    private RecyclerView chaceReView;
    private TextView upTv;
    private TextView addTv;
    CacheAdatpter cacheAdatpter;
    ZdfAdpter zdfAdpter;
    List<ZdEntity> list = new ArrayList<>();

    public static CacheBiFragment newInstance() {
        CacheBiFragment fragment = new CacheBiFragment();
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_cache_fragment;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        cacheAdatpter = new CacheAdatpter();
        chaceReView = mContentView.findViewById(R.id.chace_reView);
        chaceReView.setLayoutManager(new LinearLayoutManager(mContext));
        chaceReView.setAdapter(cacheAdatpter);
        zdfAdpter = new ZdfAdpter();
        noCacheLayout = mContentView.findViewById(R.id.no_cache_layout);
        noRecyclerview = mContentView.findViewById(R.id.no_recyclerview);
        noHTv = mContentView.findViewById(R.id.no_h_tv);
        noYjaddTv = mContentView.findViewById(R.id.no_yjadd_tv);
        cacheLayout = mContentView.findViewById(R.id.cache_layout);
        chaceReView = mContentView.findViewById(R.id.chace_reView);
        upTv = mContentView.findViewById(R.id.up_tv);
       // addTv = mContentView.findViewById(R.id.add_tv);



//        addTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                FastUtil.startActivity(mContext, EditCoinActitivy.class);
//            }
//        });
        upTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FastUtil.startActivity(mContext, EditCoinActitivy.class);
            }
        });
        noYjaddTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (ZdEntity zdEntity : zdfAdpter.getData()) {
                    CacheEntity cacheEntity = new CacheEntity(zdEntity.getCoin_name().toUpperCase(), zdEntity.getCoin_name_base());
                    CoinCache.initCoinCache().getCodeList().add(cacheEntity);
                }
                CoinCache.initCoinCache().saveData(mContext);
                noCacheLayout.setVisibility(View.GONE);
                cacheLayout.setVisibility(View.VISIBLE);
                cacheAdatpter.replaceData(CoinCache.initCoinCache().getList(mContext));


            }
        });
        noRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        noRecyclerview.setAdapter(zdfAdpter);
        noHTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = FastUtil.getRandom(20, 5);
                zdfAdpter.replaceData(list.subList(num - 5, num));
            }
        });
        if (CoinCache.initCoinCache().getList(mContext).size() > 0) {
            noCacheLayout.setVisibility(View.GONE);
            cacheLayout.setVisibility(View.VISIBLE);
            cacheAdatpter.replaceData(CoinCache.initCoinCache().getList(mContext));
        } else {
            getZDBData();
            noCacheLayout.setVisibility(View.VISIBLE);
            cacheLayout.setVisibility(View.GONE);
        }
        zdfAdpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", zdfAdpter.getData().get(position).getCoin_name_base());
                bundle.putString("symbol", zdfAdpter.getData().get(position).getCoin_name().toUpperCase());
                ARouter.getInstance().build("/hq/kline/coin").with(bundle).navigation();
            }
        });
        cacheAdatpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", cacheAdatpter.getData().get(position).getName());
                bundle.putString("symbol", cacheAdatpter.getData().get(position).getSymbol().toUpperCase());
                ARouter.getInstance().build("/hq/kline/coin").with(bundle).navigation();
            }
        });
    }

    @Subscribe
    public void update(IEvent iEvent) {
        if (iEvent instanceof CacheEntity) {
            if (CoinCache.initCoinCache().getList(mContext).size() > 0) {
                noCacheLayout.setVisibility(View.GONE);
                cacheLayout.setVisibility(View.VISIBLE);
                cacheAdatpter.replaceData(CoinCache.initCoinCache().getList(mContext));
            } else {
                getZDBData();
                noCacheLayout.setVisibility(View.VISIBLE);
                cacheLayout.setVisibility(View.GONE);
            }
        }
    }


    void getZDBData() {
        ViseHttp.GET(HqNetUrl.ZDB).baseUrl(ApiConstant.BASE_URL).tag(HqNetUrl.ZDB).request(new ACallback<BaseTResp<Zdentity1>>() {
            @Override
            public void onSuccess(BaseTResp<Zdentity1> data) {
                list.addAll(data.data.getRise_data());
                zdfAdpter.replaceData(list.subList(0, 5));
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }
}
